package com.example.experiment3intent

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView

const val EXTRA_MESSAGE = "com.example.experiment3intent.MESSAGE"
const val TAG = "CalculatorActivity"

class CalculatorActivity : AppCompatActivity() {

    private var inputTextView: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_caculator)
    }

    /**
     * 两数相加跳转结果页面
     */
    fun add(view: View) {
        // TODO: 加入默认检查。默认不能为空，弹窗toast提醒
        val text1: TextView = findViewById(R.id.editTextNumber1)
        val text2: TextView = findViewById(R.id.editTextNumber2)
        val result: Long = text1.text.toString().toLong() + text2.text.toString().toLong()
        Log.d(TAG, result.toString())
        val intent = Intent(this, ResultActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, result.toString())
        }
        startActivity(intent)
    }

    /**
     * for input buttons
     */
    fun input(view: View) {
        if (inputTextView == null) {
            // TODO: 加入toast弹窗提示，提示先点击选择按钮
            return
        }
        when (view.id) {
            R.id.button_0 -> addNumToView("0")
            R.id.button_1 -> addNumToView("1")
            R.id.button_2 -> addNumToView("2")
            R.id.button_3 -> addNumToView("3")
            R.id.button_4 -> addNumToView("4")
            R.id.button_5 -> addNumToView("5")
            R.id.button_6 -> addNumToView("6")
            R.id.button_7 -> addNumToView("7")
            R.id.button_8 -> addNumToView("8")
            R.id.button_9 -> addNumToView("9")
        }
    }

    /**
     * 一位一位的增加输入的数字
     */
    private fun addNumToView(singleNum: String) {
        Log.d(TAG, "before The string is ${inputTextView!!.text}")
        if (inputTextView!!.text.toString() == "0") {
            inputTextView!!.text = singleNum
            Log.d(TAG, "if statement execution!")
            return
        }
        // TODO: 因为布局大小限制，建议在这儿设置输入不超过九位
        // 也有add（）函数中 result超过类型Int 或 Long 大小限制的风险

        val result: String = inputTextView!!.text.toString() + singleNum
        Log.d(TAG, "The result string is $result")
        inputTextView!!.text = result
    }

    /**
     * for select button
     */
    fun select(view: View){
        if (view.id == R.id.select_button_1) {
            inputTextView = findViewById(R.id.editTextNumber1)
        } else {
            inputTextView = findViewById(R.id.editTextNumber2)
        }
        Log.d(TAG, "now select $inputTextView")
        // TODO: 使选择的按钮变色突出显示
    }

    fun backspace(view: View) {
        if (inputTextView == null) {
            // TODO: 加入toast弹窗提示，提示先点击选择按钮
            return
        }
        if (inputTextView!!.text.toString() == "0") {
            return // do nothing
        }
        val result = inputTextView!!.text.toString().dropLast(1)
        Log.d(TAG, result)
        if (result == "")
            inputTextView!!.text = "0"
        else
            inputTextView!!.text = result
    }
}
